package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.AwardfileMapper;
import com.hbnu.system.model.entity.Award;
import com.hbnu.system.model.entity.Awardfile;
import com.hbnu.system.service.AwardfileService;
import com.hbnu.system.service.IAwardService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Awardfile)表服务实现类
 *
 * @author makejava
 * @since 2023-10-15 18:41:15
 */
@Service("awardfileService")
public class AwardfileServiceImpl extends ServiceImpl<AwardfileMapper, Awardfile> implements AwardfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    IAwardService iAwardService;
    @Autowired
    IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;


    @Override
    public boolean addAwardfile(Awardfile awardfile) {
        Award byId = iAwardService.getById(awardfile.getAwardid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        awardfile.setFiledate(LocalDateTime.now());
        if(baseMapper.insert(awardfile) != 1)
            throw new BaseException("上传失败,请重新尝试");
        return true;
    }

    @Override
    public Awardfile getAwardfile(String id) {
        Awardfile awardfile = super.getOne(Wrappers.lambdaQuery(Awardfile.class)
                .eq(Awardfile::getFileid,id));
        if(Objects.isNull(awardfile)){
            throw new BaseException("文件编号异常!未查找到该文件信息");
        }
        return awardfile;
    }

    @Override
    public List<Awardfile> pageAwardfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("成果编号为空值");
        }
        List<Awardfile> awardfileList = baseMapper.selectList(Wrappers.lambdaQuery(Awardfile.class)
                .eq(Awardfile::getAwardid,id));
        return awardfileList;
    }

    @Override
    public boolean deleteAwardfile(String id) throws IOException {
        Awardfile one = super.getOne(Wrappers.lambdaQuery(Awardfile.class)
                .eq(Awardfile::getFileid, id));
        Award byId = iAwardService.getById(one.getAwardid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }


        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateAwardfile(Awardfile awardfile) {
        Award byId = iAwardService.getById(awardfile.getAwardid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        return baseMapper.updateById(awardfile)==1;
    }
}

